﻿using System;
using System.Configuration;

namespace Job.Core.Common
{
    /// <summary>
    /// 以编程的方式访问 objectFactoryConfigSection 配制节(无法继承此类)。
    /// </summary>
    public sealed class ObjectFactoryConfigSection : ConfigurationSection
    {
        private const string typesName = "types";
        private const string namedsName = "nameds";

        /// <summary>
        /// 类型工厂配制节。
        /// </summary>
        [ConfigurationProperty(typesName)]
        public ObjectFactoryTypeSettingsCollection Types
        {
            get { return base[typesName] as ObjectFactoryTypeSettingsCollection; }
        }

        /// <summary>
        /// 命名工厂配制节。 
        /// </summary>
        [ConfigurationProperty(namedsName)]
        public ObjectFactoryNamedSettingsCollection Nameds
        {
            get { return base[namedsName] as ObjectFactoryNamedSettingsCollection; }
        }
    }

    /// <summary>
    /// 以编程的方式访问 objectFactoryConfigSection/param 配制节。
    /// </summary>
    public sealed class ObjectFactoryParam : ConfigurationElement
    {
        private const string type = "type";
        private const string value = "value";

        /// <summary>
        /// 类型。
        /// </summary>
        [ConfigurationProperty(type)]
        public string Type
        {
            get { return base[type] as string; }
        }        

        /// <summary>
        /// 参数值。
        /// </summary>
        [ConfigurationProperty(value, IsRequired=true)]
        public string Value
        {
            get { return base[value] as string; }
        }
    }

    /// <summary>
    /// 以编程的方式访问 objectFactoryConfigSection/params 配制节。
    /// </summary>
    [ConfigurationCollection(typeof(ObjectFactoryParam))]
    public sealed class ObjectFactoryParamCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ObjectFactoryParam();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return element;
        }
    }

    /// <summary>
    /// 以编程的方式访问 objectFactoryConfigSection/types 配制节。
    /// </summary>
    public sealed class ObjectFactoryTypeSettings : ConfigurationSection
    {
        private const string src = "src";
        private const string target = "target";
        private const string singleton = "singleton";
        private const string @params = "params";

        /// <summary>
        /// 源类型。
        /// </summary>
        [ConfigurationProperty(src, IsRequired=true, IsKey=true)]
        public string Src
        {
            get { return base[src] as string; }
        }

        /// <summary>
        /// 目标类型。
        /// </summary>
        [ConfigurationProperty(target, IsRequired = true)]
        public string Target
        {
            get { return base[target] as string; }
        }

        /// <summary>
        /// 是否为单例。
        /// </summary>
        [ConfigurationProperty(singleton)]
        public bool Singleton
        {
            get { return Convert.ToBoolean(base[singleton]); }
        }

        /// <summary>
        /// 构造参数，请注意顺序。
        /// </summary>
        [ConfigurationProperty(@params)]
        public ObjectFactoryParamCollection Params
        {
            get { return base[@params] as ObjectFactoryParamCollection; }
        }
    }

    /// <summary>
    /// 以编程的方式访问 objectFactoryConfigSection/types 配制节。
    /// </summary>
    [ConfigurationCollection(typeof(ObjectFactoryTypeSettings))]
    public sealed class ObjectFactoryTypeSettingsCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ObjectFactoryTypeSettings();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ObjectFactoryTypeSettings)element).Src;
        }

        /// <summary>
        /// 获取指定类型的配制设置。
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ObjectFactoryTypeSettings this[Type type]
        {
            get { return this[type.FullName]; }
        }

        /// <summary>
        /// 获取指定类型名称的配制设置。
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public new ObjectFactoryTypeSettings this[string typeName]
        {
            get { return BaseGet(typeName) as ObjectFactoryTypeSettings; }
        }
    }

    /// <summary>
    /// 以编程的方式访问 objectFactoryConfigSection/nameds 配制节。
    /// </summary>
    public sealed class ObjectFactoryNamedSettings : ConfigurationSection
    {
        private const string name = "name";
        private const string type = "type";
        private const string singleton = "singleton";
        private const string @params = "params";

        /// <summary>
        /// 名称。 
        /// </summary>
        [ConfigurationProperty(name, IsRequired=true, IsKey=true)]
        public string Name
        {
            get { return base[name] as string; }
        }

        /// <summary>
        /// 类型。
        /// </summary>
        [ConfigurationProperty(type, IsRequired=true)]
        public string Type
        {
            get { return base[type] as string; }
        }

        /// <summary>
        /// 是否为单例。
        /// </summary>
        [ConfigurationProperty(singleton)]
        public bool Singleton
        {
            get { return Convert.ToBoolean(base[singleton]); }
        }

        /// <summary>
        /// 构造参数，请注意顺序。
        /// </summary>
        [ConfigurationProperty(@params)]
        public ObjectFactoryParamCollection Params
        {
            get { return base[@params] as ObjectFactoryParamCollection; }
        }
    }

    /// <summary>
    /// 以编程的方式访问 objectFactoryConfigSection/nameds 配制节。
    /// </summary>
    [ConfigurationCollection(typeof(ObjectFactoryNamedSettings))]
    public sealed class ObjectFactoryNamedSettingsCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ObjectFactoryNamedSettings();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ObjectFactoryNamedSettings)element).Name;
        }

        /// <summary>
        /// 获取指定名称的配制设置。
        /// </summary>
        /// <param name="name">名称。</param>
        /// <returns></returns>
        public new ObjectFactoryNamedSettings this[string name]
        {
            get { return BaseGet(name) as ObjectFactoryNamedSettings; }
        }
    }
}